package com.hhss.qishi.controller.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hhss.qishi.config.constant.ModuleConstants;
import com.hhss.qishi.config.constant.OperateConstants;
import com.hhss.qishi.config.log.webLog.WebLog;
import com.hhss.qishi.config.pagehelper.PageVO;
import com.hhss.qishi.controller.CommonController;
import com.hhss.qishi.entity.system.Role;
import com.hhss.qishi.service.system.RoleService;
import com.hhss.qishi.utils.ConvertPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 角色控制器
 * @author hhss
 * @date 2019年3月30日
 */
@RestController
@RequestMapping("roles")
public class RoleController extends CommonController<Role> {
	
	@Autowired
    private RoleService roleService;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
    public Map<String, Object> getList(Role role) {
        Page<Role> page = PageHelper.startPage(role.getPage(), role.getLimit());
		roleService.getList(role);
        PageVO<Role> vo = PageVO.build(page);
        return ConvertPageUtil.Convert(vo);
    }

	@RequestMapping(value = "/", method = RequestMethod.POST)
	@WebLog(module = ModuleConstants.MODULE_ROLE, operate = OperateConstants.OPR_ADD)
	public int add(Role role) {
		return roleService.add(role);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Role getById(@PathVariable Long id) {
		return roleService.getById(id);
	}
	
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@WebLog(module = ModuleConstants.MODULE_ROLE, operate = OperateConstants.OPR_UPDATE)
	public int update(Role role) {
		return roleService.updateById(role);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@WebLog(module = ModuleConstants.MODULE_ROLE, operate = OperateConstants.OPR_DELETE)
	public int deleteById(@PathVariable Long id) {
		return roleService.deleteById(id);
	}
	
	@RequestMapping(value = "/permissions", method = RequestMethod.GET)
	public Role getPermissionRoleById(Role role) {
		return roleService.getPermissionRoleById(role);
	}
}